package com.lemon.cloud.mp.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.mp.entity.WxMsg;
import com.lemon.cloud.mp.service.WxMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/wxfansmsg")
public class WxFansMsgController {

    @Autowired
    private WxMsgService wxMsgService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param msg 查询条件
     */
    @GetMapping("/page")
    public ResultMsg<IPage<WxMsg>> getWxMsgPage(Page<WxMsg> page, WxMsg msg) {
        return ResultMsg.resultSuccess(wxMsgService.page(page, Wrappers.query(msg)));
    }

    /**
     * 通过id查询微信消息
     * @param id id
     * @return ResultMsg
     */
    @GetMapping("/{id}")
    public ResultMsg<WxMsg> getById(@PathVariable("id") Integer id) {
        return ResultMsg.resultSuccess(wxMsgService.getById(id));
    }

    /**
     * 新增微信消息
     * @param wxMsg 微信消息
     * @return ResultMsg
     */
    @PostMapping
    @PreAuthorize("hasAuthority('mp_wxmsg_add')")
    public ResultMsg save(@RequestBody WxMsg wxMsg) {
        return wxMsgService.saveAndPushMsg(wxMsg);
    }

    /**
     * 修改微信消息
     * @param wxMsg 微信消息
     * @return ResultMsg
     */
    @PutMapping
    @PreAuthorize("hasAuthority('mp_wxmsg_edit')")
    public ResultMsg<Boolean> updateById(@RequestBody WxMsg wxMsg) {
        return ResultMsg.resultSuccess(wxMsgService.updateById(wxMsg));
    }

    /**
     * 通过id删除微信消息
     * @param id id
     * @return ResultMsg
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('mp_wxmsg_del')")
    public ResultMsg<Boolean> removeById(@PathVariable("id") String id) {
        return ResultMsg.resultSuccess(wxMsgService.removeById(id));
    }

}
